package com.ruoyi.system.service.impl;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.DeptFileArchived;
import com.ruoyi.system.domain.MeetingManagement;
import com.ruoyi.system.domain.vo.UserUnDoVo;
import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.system.service.IDeptFileArchivedService;
import com.ruoyi.system.service.IMeetingManagementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.MeetingSummaryMapper;
import com.ruoyi.system.domain.MeetingSummary;
import com.ruoyi.system.service.IMeetingSummaryService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

/**
 * 会议纪要Service业务层处理
 *
 * @author ruoyi
 * @date 2024-04-03
 */
@Service
public class MeetingSummaryServiceImpl implements IMeetingSummaryService
{
    @Autowired
    private MeetingSummaryMapper meetingSummaryMapper;
    @Autowired
    private IMeetingManagementService meetingManagementService;
    @Autowired
    private IDeptFileArchivedService deptFileArchivedService;
    @Autowired
    private SysUserMapper sysUserMapper;

    /**
     * 查询会议纪要
     *
     * @param id 会议纪要主键
     * @return 会议纪要
     */
    @Override
    public MeetingSummary selectMeetingSummaryById(Long id)
    {
        MeetingSummary summary = meetingSummaryMapper.selectMeetingSummaryById(id);
        //纪要文件
        String summaryFile = summary.getSummaryFile();
        if(StringUtils.isNotBlank(summaryFile)){
            List<String> summaryFileList = Arrays.asList(summaryFile.split(","));
            summary.setSummaryFileList(summaryFileList);
        }
        //会议记录文件
        String recordFile = summary.getRecordFile();
        if(StringUtils.isNotBlank(recordFile)){
            List<String> recordFileList = Arrays.asList(recordFile.split(","));
            summary.setRecordFileList(recordFileList);
        }
        //纪要图片
        String summaryImage = summary.getSummaryImage();
        if(StringUtils.isNotBlank(summaryImage)){
            List<String> summaryImageList = Arrays.asList(summaryImage.split(","));
            summary.setSummaryImageList(summaryImageList);
        }
        //会议通知表
        String meetingNotify = summary.getMeetingNotify();
        if(StringUtils.isNotBlank(meetingNotify)){
            List<String> meetingNotifyList = Arrays.asList(meetingNotify.split(","));
            summary.setMeetingNotifyList(meetingNotifyList);
        }
        //会议签到表
        String meetingSignature = summary.getMeetingSignature();
        if(StringUtils.isNotBlank(meetingSignature)){
            List<String> meetingSignatureList = Arrays.asList(meetingSignature.split(","));
            summary.setMeetingSignatureList(meetingSignatureList);
        }
        return summary;
    }

    /**
     * 根据条件查询会议纪要列表
     *
     * @param meetingManagement 会议纪要
     * @return 会议纪要集合
     */
    @Override
    public List<MeetingManagement> getMeetingSummaryList(MeetingManagement meetingManagement){
        List<MeetingManagement> meetingManagementList = meetingManagementService.selectMeetingSignaturelist(meetingManagement);
        //已召开的会议
        List<MeetingManagement> collect = meetingManagementList.stream().filter(o -> o.getStatus().equals("2") || o.getStatus().equals("3")).collect(Collectors.toList());
        for(MeetingManagement meeting : collect){
            Long id = meeting.getId();
            MeetingSummary summary = meetingSummaryMapper.selectMeetingSummaryByMeetingId(id);
            meeting.setMeetingSummary(summary);
        }
        return collect;
    }

    /**
     * 查询会议纪要列表
     *
     * @param meetingSummary 会议纪要
     * @return 会议纪要
     */
    @Override
    public List<MeetingSummary> selectMeetingSummaryList(MeetingSummary meetingSummary)
    {
        List<MeetingSummary> meetingSummarieList = meetingSummaryMapper.selectMeetingSummaryList(meetingSummary);
        return meetingSummarieList;
    }

    /**
     * 新增会议纪要
     *
     * @param meetingSummary 会议纪要
     * @return 结果
     */
    @Override
    public int insertMeetingSummary(MeetingSummary meetingSummary)
    {
        meetingSummary.setCreateTime(DateUtils.getNowDate());
        return meetingSummaryMapper.insertMeetingSummary(meetingSummary);
    }

    /**
     * 修改会议纪要
     *
     * @param meetingSummary 会议纪要
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateMeetingSummary(MeetingSummary meetingSummary)
    {
        MeetingSummary summary = meetingSummaryMapper.selectMeetingSummaryById(meetingSummary.getId());
        String createBy = meetingSummary.getCreateBy();
        SysUser user = sysUserMapper.selectUserById(Long.valueOf(createBy));
        String userName = user.getNickName();
        //获取会议归纳文件夹信息
        DeptFileArchived deptFileArchived = deptFileArchivedService.selectDeptFileArchivedByAssociationId(String.valueOf(summary.getMeetingId()));
        //如果是上传之后重新编辑资料，删除归纳文件重新上传
        DeptFileArchived deptFileArchivedSummary = deptFileArchivedService.selectDeptFileArchivedByAssociationId(String.valueOf(meetingSummary.getId()));
        if(!ObjectUtils.isEmpty(deptFileArchivedSummary)){
            deptFileArchivedService.deleteDeptFileArchivedByAssociationId(deptFileArchivedSummary.getAssociationId());
        }
        //纪要文件
        List<String> summaryFileList = meetingSummary.getSummaryFileList();
        if(!CollectionUtils.isEmpty(summaryFileList)){
            String summaryFile = StringUtils.join(summaryFileList, ",");
            meetingSummary.setSummaryFile(summaryFile);
            //创建会议纪要文件夹
            DeptFileArchived deptFileArchivedVo = new DeptFileArchived();
            deptFileArchivedVo.setDeptId(deptFileArchived.getDeptId());
            deptFileArchivedVo.setCreateBy(createBy);
            deptFileArchivedVo.setCreateName(userName);
            deptFileArchivedVo.setFileType("0");
            deptFileArchivedVo.setFileName("会议纪要文件");
            deptFileArchivedVo.setParentId(deptFileArchived.getParentId());
            deptFileArchivedVo.setAssociationId(String.valueOf(meetingSummary.getId()));
            Long archivedId = deptFileArchivedService.insertDeptFileArchived(deptFileArchivedVo);
            //创建会议纪要文件
            DeptFileArchived deptFileArchivedVo2 = new DeptFileArchived();
            deptFileArchivedVo2.setDeptId(deptFileArchived.getDeptId());
            deptFileArchivedVo2.setCreateBy(createBy);
            deptFileArchivedVo2.setCreateName(userName);
            deptFileArchivedVo2.setFileType("1");
            deptFileArchivedVo2.setFileName("会议纪要文件附件");
            deptFileArchivedVo2.setFileUrl(summaryFile);
            deptFileArchivedVo2.setAssociationId(String.valueOf(meetingSummary.getId()));
            deptFileArchivedVo2.setParentId(archivedId);
            deptFileArchivedService.insertDeptFileArchived(deptFileArchivedVo2);
        }
        //会议记录文件
        List<String> recordFileList = meetingSummary.getRecordFileList();
        if(!CollectionUtils.isEmpty(recordFileList)){
            String recordFile = StringUtils.join(recordFileList, ",");
            meetingSummary.setRecordFile(recordFile);
            //创建会议记录文件夹
            DeptFileArchived deptFileArchivedVo = new DeptFileArchived();
            deptFileArchivedVo.setDeptId(deptFileArchived.getDeptId());
            deptFileArchivedVo.setCreateBy(createBy);
            deptFileArchivedVo.setCreateName(userName);
            deptFileArchivedVo.setFileType("0");
            deptFileArchivedVo.setFileName("会议记录文件");
            deptFileArchivedVo.setParentId(deptFileArchived.getParentId());
            deptFileArchivedVo.setAssociationId(String.valueOf(meetingSummary.getId()));
            Long archivedId = deptFileArchivedService.insertDeptFileArchived(deptFileArchivedVo);
            //创建会议记录文件
            DeptFileArchived deptFileArchivedVo2 = new DeptFileArchived();
            deptFileArchivedVo2.setDeptId(deptFileArchived.getDeptId());
            deptFileArchivedVo2.setCreateBy(createBy);
            deptFileArchivedVo2.setCreateName(userName);
            deptFileArchivedVo2.setFileType("1");
            deptFileArchivedVo2.setFileName("会议记录文件附件");
            deptFileArchivedVo2.setFileUrl(recordFile);
            deptFileArchivedVo2.setAssociationId(String.valueOf(meetingSummary.getId()));
            deptFileArchivedVo2.setParentId(archivedId);
            deptFileArchivedService.insertDeptFileArchived(deptFileArchivedVo2);
        }
        //纪要图片
        List<String> summaryImageList = meetingSummary.getSummaryImageList();
        if(!CollectionUtils.isEmpty(summaryImageList)){
            String summaryImage = StringUtils.join(summaryImageList, ",");
            meetingSummary.setSummaryImage(summaryImage);
            //创建纪要图片文件夹
            DeptFileArchived deptFileArchivedVo = new DeptFileArchived();
            deptFileArchivedVo.setDeptId(deptFileArchived.getDeptId());
            deptFileArchivedVo.setCreateBy(createBy);
            deptFileArchivedVo.setCreateName(userName);
            deptFileArchivedVo.setFileType("0");
            deptFileArchivedVo.setFileName("会议图片");
            deptFileArchivedVo.setParentId(deptFileArchived.getParentId());
            deptFileArchivedVo.setAssociationId(String.valueOf(meetingSummary.getId()));
            Long archivedId = deptFileArchivedService.insertDeptFileArchived(deptFileArchivedVo);
            //创建纪要图片文件
            DeptFileArchived deptFileArchivedVo2 = new DeptFileArchived();
            deptFileArchivedVo2.setDeptId(deptFileArchived.getDeptId());
            deptFileArchivedVo2.setCreateBy(createBy);
            deptFileArchivedVo2.setCreateName(userName);
            deptFileArchivedVo2.setFileType("1");
            deptFileArchivedVo2.setFileName("会议图片附件");
            deptFileArchivedVo2.setFileUrl(summaryImage);
            deptFileArchivedVo2.setAssociationId(String.valueOf(meetingSummary.getId()));
            deptFileArchivedVo2.setParentId(archivedId);
            deptFileArchivedService.insertDeptFileArchived(deptFileArchivedVo2);
        }
        //会议通知表
        List<String> meetingNotifyList = meetingSummary.getMeetingNotifyList();
        if(!CollectionUtils.isEmpty(meetingNotifyList)){
            String meetingNotify = StringUtils.join(meetingNotifyList, ",");
            meetingSummary.setMeetingNotify(meetingNotify);
            //创建会议通知表文件夹
            DeptFileArchived deptFileArchivedVo = new DeptFileArchived();
            deptFileArchivedVo.setDeptId(deptFileArchived.getDeptId());
            deptFileArchivedVo.setCreateBy(createBy);
            deptFileArchivedVo.setCreateName(userName);
            deptFileArchivedVo.setFileType("0");
            deptFileArchivedVo.setFileName("会议通知表文件");
            deptFileArchivedVo.setParentId(deptFileArchived.getParentId());
            deptFileArchivedVo.setAssociationId(String.valueOf(meetingSummary.getId()));
            Long archivedId = deptFileArchivedService.insertDeptFileArchived(deptFileArchivedVo);
            //创建会议通知表文件
            DeptFileArchived deptFileArchivedVo2 = new DeptFileArchived();
            deptFileArchivedVo2.setDeptId(deptFileArchived.getDeptId());
            deptFileArchivedVo2.setCreateBy(createBy);
            deptFileArchivedVo2.setCreateName(userName);
            deptFileArchivedVo2.setFileType("1");
            deptFileArchivedVo2.setFileName("会议通知表附件");
            deptFileArchivedVo2.setFileUrl(meetingNotify);
            deptFileArchivedVo2.setParentId(archivedId);
            deptFileArchivedVo2.setAssociationId(String.valueOf(meetingSummary.getId()));
            deptFileArchivedService.insertDeptFileArchived(deptFileArchivedVo2);

        }
        //会议签到表
        List<String> meetingSignatureList = meetingSummary.getMeetingSignatureList();
        if(!CollectionUtils.isEmpty(meetingSignatureList)){
            String meetingSignature = StringUtils.join(meetingSignatureList, ",");
            meetingSummary.setMeetingSignature(meetingSignature);
            //创建会议签到表文件夹
            DeptFileArchived deptFileArchivedVo = new DeptFileArchived();
            deptFileArchivedVo.setDeptId(deptFileArchived.getDeptId());
            deptFileArchivedVo.setCreateBy(createBy);
            deptFileArchivedVo.setCreateName(userName);
            deptFileArchivedVo.setFileType("0");
            deptFileArchivedVo.setFileName("会议签到表文件");
            deptFileArchivedVo.setParentId(deptFileArchived.getParentId());
            deptFileArchivedVo.setAssociationId(String.valueOf(meetingSummary.getId()));
            Long archivedId = deptFileArchivedService.insertDeptFileArchived(deptFileArchivedVo);
            //创建会议签到表文件
            DeptFileArchived deptFileArchivedVo2 = new DeptFileArchived();
            deptFileArchivedVo2.setDeptId(deptFileArchived.getDeptId());
            deptFileArchivedVo2.setCreateBy(createBy);
            deptFileArchivedVo2.setCreateName(userName);
            deptFileArchivedVo2.setFileType("1");
            deptFileArchivedVo2.setFileName("会议签到表附件");
            deptFileArchivedVo2.setFileUrl(meetingSignature);
            deptFileArchivedVo2.setParentId(archivedId);
            deptFileArchivedVo2.setAssociationId(String.valueOf(meetingSummary.getId()));
            deptFileArchivedService.insertDeptFileArchived(deptFileArchivedVo2);
        }
        //上传时间
        String summaryFile1 = summary.getSummaryFile();
        String summaryImage1 = summary.getSummaryImage();
        if(StringUtils.isBlank(summaryFile1)&&StringUtils.isBlank(summaryImage1)){
            Date nowDate = DateUtils.getNowDate();
            meetingSummary.setUploadTime(nowDate);
        }
        meetingSummary.setUpdateTime(DateUtils.getNowDate());
        return meetingSummaryMapper.updateMeetingSummary(meetingSummary);
    }

    /**
     * 删除会议纪要信息
     *
     * @param id 会议纪要主键
     * @return 结果
     */
    @Override
    public int deleteMeetingSummaryById(Long id)
    {
        return meetingSummaryMapper.deleteMeetingSummaryById(id);
    }

    /**
     * 修改会议纪要状态
     *
     * @param meetingSummary 会议纪要
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateMeetingSummaryStatus(MeetingSummary meetingSummary){
        //修改会议纪要状态
        MeetingSummary summary = new MeetingSummary();
        summary.setId(meetingSummary.getId());
        summary.setStatus(meetingSummary.getStatus());
        int i = meetingSummaryMapper.updateMeetingSummary(summary);
        //完成和延期完成的修改会议状态为 已召开已归档
        String status = meetingSummary.getStatus();
        if("1".equals(status)||"3".equals(status)){
            Long meetingId = meetingSummary.getMeetingId();
            MeetingManagement meetingManagement = new MeetingManagement();
            meetingManagement.setId(meetingId);
            meetingManagement.setStatus("3");
            meetingManagementService.updateMeetingManagement(meetingManagement);
        }
        return i;
    }

    /**
     * 查询文件上传待办任务
     *
     * @return 结果
     */
    @Override
    public List<UserUnDoVo> selectMeetingUploadTask(UserUnDoVo userUnDoVo){
        List<UserUnDoVo> userUnDoVos = meetingSummaryMapper.selectMeetingUploadTask(userUnDoVo);
        return userUnDoVos;
    }
}
